package com.ids.transfer.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
	
	private static SimpleDateFormat shortFormatter = new SimpleDateFormat(
			"yyyy-MM-dd");

	private static SimpleDateFormat longFormatter = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	public static final String getNowTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter.format(new Date());
	}

	public static final String getNowTime(String partern) {
		SimpleDateFormat formatter = new SimpleDateFormat(partern);
		return formatter.format(new Date());
	}

	public static final String getNowTime(SimpleDateFormat formatter) {
		return formatter.format(new Date());
	}

	public static String dateToSimpleString(Date dDate) {
		if (dDate == null) {
			return null;
		}
		return dateToString(dDate, "yyyy-MM-dd");
	}

	public static String dateToString(Date dDate) {
		if (dDate == null) {
			return null;
		}
		return dateToString(dDate, "yyyy-MM-dd HH:mm:ss");
	}

	public static String dateToString(Date dDate, String pattern) {
		if (dDate == null) {
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		try {
			return formatter.format(dDate);
		} catch (Exception ex) {
		}
		return null;
	}

	public static Date simpleStringToDate(String dateStr) {
		if (dateStr == null) {
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return formatter.parse(dateStr);
		} catch (ParseException ex) {
		}
		return null;
	}

	public static Date stringToDate(String dateStr) {
		if (dateStr == null) {
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return formatter.parse(dateStr);
		} catch (ParseException ex) {
		}
		return null;
	}

	public static Date stringToDate(String dateStr, String pattern) {
		if (dateStr == null) {
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		try {
			return formatter.parse(dateStr);
		} catch (ParseException ex) {
		}
		return null;
	}

	public static Timestamp strToTimestamp(String dateStr) {
		if (dateStr == null)
			return null;
		try {
			return new Timestamp(longFormatter.parse(dateStr).getTime());
		} catch (ParseException ex) {
		}
		return null;
	}

	public static Timestamp simpleStrToTimestamp(String dateStr) {
		if (dateStr == null)
			return null;
		try {
			return new Timestamp(shortFormatter.parse(dateStr).getTime());
		} catch (ParseException ex) {
		}
		return null;
	}

	public static Timestamp strToTimestamp(String dateStr, String pattern) {
		if (dateStr == null) {
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		try {
			return new Timestamp(formatter.parse(dateStr).getTime());
		} catch (ParseException ex) {
		}
		return null;
	}

	public static final String toDBString(String str) {
		if (str == null) {
			return null;
		}
		String strsql = "";
		for (int i = 0; i < str.length(); i++) {
			String sqldesc = str.substring(i, i + 1);
			if (sqldesc.equals("'"))
				sqldesc = "''";
			strsql = strsql + sqldesc;
		}
		return strsql;
	}

	public static String toDBSimpleString(Date date) {
		return toOracleString(date, "yyyy-MM-dd", "yyyy-MM-dd");
	}

	public static String toOracleString(Date date) {
		return toOracleString(date, "yyyy-MM-dd HH:mm:ss",
				"yyyy-MM-dd HH24:mi:ss");
	}

	public static String toOracleSimpleString(Date date) {
		return toOracleString(date, "yyyy-MM-dd HH:mm:ss",
				"yyyy-MM-dd HH24:mi:ss");
	}

	public static String toSybaseString(Date date) {
		return toSybaseString(date, "yyyy-MM-dd HH:mm:ss");
	}

	public static String toSybaseSimpleString(Date date) {
		return toSybaseString(date, "yyyy-MM-dd");
	}

	private static String toSybaseString(Date date, String pattern) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		return " '" + formatter.format(date) + "' ";
	}

	private static String toOracleString(Date date, String javaPattern,
			String dbPattern) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(javaPattern);
		return " to_date('" + formatter.format(date) + "','" + dbPattern
				+ "') ";
	}

	public static void main(String[] args) {
		System.out.println("getNowTime=" + getNowTime());
		System.out.println("dateToString=" + dateToString(new Date()));
		System.out.println("stringToDate="
				+ stringToDate("2004-05-13 12:00:00"));
		System.out.println("toOracleSimpleString="
				+ toDBSimpleString(new Date()));
		System.out.println("toSybaseString=" + toSybaseString(new Date()));
		System.out.println("toSybaseSimpleString="
				+ toSybaseSimpleString(new Date()));
	}
}